package com.xyq.fs.search;

import java.io.IOException;

import java.util.Date;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.LeafReaderContext;

import org.apache.lucene.queries.CustomScoreProvider;
import org.apache.lucene.queries.CustomScoreQuery;
import org.apache.lucene.search.Query;

import com.xyq.fs.constants.FieldConstant;

@SuppressWarnings("deprecation")
public class MyScoreUtil extends CustomScoreQuery {

	private long nowTime;

	{
		Date d = new Date();
		nowTime = d.getTime();

	}

	private String[] keys;

	private Query subQuery;

	public String[] getKeys() {
		return keys;
	}

	public void setKeys(String[] keys) {
		this.keys = keys;
	}

	public Query getSubQuery() {
		return subQuery;
	}

	public void setSubQuery(Query subQuery) {
		this.subQuery = subQuery;

	}

	public MyScoreUtil(Query subQuery, String[] keys) {

		super(subQuery);
		this.keys = keys;

	}

	private class RecencyBooster extends CustomScoreProvider {

		public RecencyBooster(LeafReaderContext context) throws IOException {
			super(context);

		}

		public float customScore(int doc, float subQueryScore, float valSrcScore) throws IOException {

			Document docc = context.reader().document(doc);

			// 第一权重：备注权限(现在时间+1)
			if (docc.get(FieldConstant.FILE_BZ_SORT) != null) {
				valSrcScore = nowTime + 1;
			}
			// 第二权重 ：文件修改时间权重
			valSrcScore += Float.parseFloat(docc.get(FieldConstant.FILE_UPDATE_TIME));
			return valSrcScore;

		}
	}

	public CustomScoreProvider getCustomScoreProvider(LeafReaderContext context) throws IOException {

		return new RecencyBooster(context);
	}

	public static void main(String[] args) {

		Date d = new Date();
		System.out.println(d.getTime());
	}
}
